package com.nineclock.approve.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.nineclock.approve.nodeResolver.ProcessNodeResolver;
import com.nineclock.approve.nodeResolver.ProcessNodeResolverFactory;
import com.nineclock.approve.pojo.ApproveDefinition;
import com.nineclock.approve.service.ActivitiService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.BpmnAutoLayout;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Slf4j
public class ActivitiServiceImpl implements ActivitiService {

    @Autowired
    RepositoryService repositoryService;

    @Override
    public void deployBpmnToActiviti(ApproveDefinition definition) {
        //构建 bpmnModel
        BpmnModel bpmnModel = new BpmnModel();

        //创建流程对象
        Process process = new Process();
        process.setId("process_"+ definition.getId());
        // 构建流程对象中的节点
        String flowJson = definition.getFlowJson();
        JSONArray jsonArray = JSONUtil.parseArray(flowJson);
        addNodeToActiviti(process, jsonArray);

        //添加 流程对象到 model中
        bpmnModel.addProcess(process);

        //执行自动布局操作
        new BpmnAutoLayout(bpmnModel).execute();

        //输出XML
        BpmnXMLConverter converter = new BpmnXMLConverter();
        byte[] bytes = converter.convertToXML(bpmnModel);
        System.out.println(new String(bytes));

        //部署到 activiti中
        repositoryService
                .createDeployment()
                .addBpmnModel(definition.getName()+".bpmn", bpmnModel)
                .name(definition.getName()).deploy();
    }

    @Override
    public void dropBpmnFromActiviti(ApproveDefinition definition) {
        //查询流程部署对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("process_" + definition.getId()).singleResult();
        //根据部署id删除流程对象
        repositoryService.deleteDeployment(processDefinition.getDeploymentId());
    }

    private void addNodeToActiviti(Process process, JSONArray jsonArray) {
        if(jsonArray != null && jsonArray.size() > 0){
            for (int i = 0; i < jsonArray.size() ; i++) {
                //当前节点
                JSONObject currNode = (JSONObject) jsonArray.get(i);
                //下一个节点
                JSONObject nextNode = null;
                // 保证有下一个节点
                if( i < jsonArray.size() - 1){
                    nextNode = (JSONObject) jsonArray.get(i + 1);
                }
                //每个节点的类型
                String type = currNode.getStr("type");
                ProcessNodeResolver nodeResolver = ProcessNodeResolverFactory.getInstance(type);

                nodeResolver.addFlowNodeToActiviti(process, currNode, nextNode);
            }
        }
    }
}
